﻿using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace Linker.TaskCenter.EntityFrameworkCore
{
    public class
        TaskCenterMigrationsDbContextFactory : IDesignTimeDbContextFactory<TaskCenterDbContext>
    {
        public TaskCenterDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<TaskCenterDbContext>().UseMySql(
                configuration.GetConnectionString("Default"), new MySqlServerVersion(new Version(5,7,23)));

            return new TaskCenterDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Linker.TaskCenter.HttpApi.Host/"))
                .AddJsonFile("appsettings.json", false);

            return builder.Build();
        }
    }
}